package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.*;
import cn.wolfcode.car.business.query.GoodsQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IGoodsCategoryService;
import cn.wolfcode.car.business.service.IGoodsService;
import cn.wolfcode.car.business.service.IStoreService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.json.ZTreeJson;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.aspectj.weaver.loadtime.Aj;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/goods")
public class GoodsController {
    //模板前缀
    private static final String prefix = "business/goods/";

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IGoodsCategoryService goodsCategoryService;




    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:goods:view")
    @RequestMapping("/listPage")
    public String listPage(Model model){
        List<Store> stores = storeService.selectAll();
        model.addAttribute("stores",stores);
        List<GoodsCategory> goodsCategories = goodsCategoryService.list();
        model.addAttribute("goodsCategories",goodsCategories);
        return prefix + "list";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:goods:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Goods> query(GoodsQuery qo){
        return goodsService.query(qo);
    }

    //物品添加页面
    @RequestMapping("/addPage")
    public String addPage(Model model){
        List<GoodsCategory> goodsCategories = goodsCategoryService.list();
        model.addAttribute("goodsCategories",goodsCategories);
        return prefix+"add";
    }

    /**
     * 添加页面
     * @param goods
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Goods goods){
        goodsService.save(goods);
        return AjaxResult.success();
    }

    /**
     * 修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model,Long id){
        Goods goods = goodsService.get(id);
        model.addAttribute("goods",goods);
        return prefix+"edit";
    }

    /**
     * 修改
     * @param goods
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Goods goods){
        goodsService.update(goods);
        return AjaxResult.success();
    }

    /**
     * 上传图片文件
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/uploading")
    @ResponseBody
    public AjaxResult uploading(MultipartFile file) throws Exception {
        //接收文件
        String path = goodsService.uploading(file);
        return AjaxResult.success("操作成功",path);
    }

    /**
     * 读取图片文件
     * @param response
     * @param id
     * @throws IOException
     */
    @RequestMapping("/readResource")
    @ResponseBody
    public AjaxResult readResource(HttpServletResponse response,Long id) throws IOException {
        String inputStream = goodsService.readResource(id);
       //IOUtils.copy(inputStream,response.getOutputStream());
        return AjaxResult.success("查看图片",inputStream);
    }

    /**
     * 删除功能
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id){
        goodsService.remove(id);
        return AjaxResult.success();
    }

    @RequestMapping("goodsItemPage")
    public String goodsItemPage(Long id,Model model){
        model.addAttribute("id",id);
        return "business/storeDetail/list.html";
    }

    //输入库明细
    @RequestMapping("/goodsItem")
    @ResponseBody
    public TablePageInfo<GoodsItem> goodsItem(Long id, QueryObject qo){
        return goodsService.goodsItem(id,qo);

    }

}
